/*
 * Esta janela servirá para exibir os resultados da pesquisa de funcionario.
 */

/*
 * Lista.java
 *
 * Created on 10/03/2011, 14:50:43
 */

package gui;

import dominio.Funcionario;
import java.util.List;

/**
 *
 * @author nathalie.ramos
 */
public class JanelaListaFuncionario extends javax.swing.JFrame implements JanelaLista{

    /** Creates new form Lista */
    public JanelaListaFuncionario() {
        initComponents();
    }

    public JanelaListaFuncionario(ControladorFuncionario controlador, List lista){
        this.controlador = controlador;
        this.lista = lista;
        initComponents();
        setTitle("Lista de Funcionários");
    }

    /** Este método é chamado pelo construtor para
     * inicializar a  janela.
     */
    @SuppressWarnings("unchecked")

    private void initComponents() {

        painelTabela = new javax.swing.JPanel();
        painelDeRolagem = new javax.swing.JScrollPane();
        tabelaResultado = new javax.swing.JTable();
        painelBotoes = new javax.swing.JPanel();
        botaoEscolher = new javax.swing.JButton();
        botaoEscolher.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                controlador.setObject( (Funcionario) lista.get( tabelaResultado.getSelectedRow() ));
                controlador.preparaJanelaCadastro();
                dispose();
            }
        });

        botaoCancelar = new javax.swing.JButton();
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        painelTabela.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(204, 204, 255), null));

        tabelaResultado.setBackground(new java.awt.Color(252, 252, 252));
        tabelaResultado.setModel(new javax.swing.table.DefaultTableModel(
            preparaTabela(),
            new String [] {
                "Nome", "Área de Atuação"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaResultado.getTableHeader().setReorderingAllowed(false);
        painelDeRolagem.setViewportView(tabelaResultado);
        tabelaResultado.getColumnModel().getColumn(0).setResizable(false);

        javax.swing.GroupLayout painelTabelaLayout = new javax.swing.GroupLayout(painelTabela);
        painelTabela.setLayout(painelTabelaLayout);
        painelTabelaLayout.setHorizontalGroup(
            painelTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelTabelaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelDeRolagem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        painelTabelaLayout.setVerticalGroup(
            painelTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelTabelaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelDeRolagem, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        painelBotoes.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(204, 204, 255), null));

        botaoEscolher.setText("Escolher");

        botaoCancelar.setText("Cancelar");

        javax.swing.GroupLayout painelBotoesLayout = new javax.swing.GroupLayout(painelBotoes);
        painelBotoes.setLayout(painelBotoesLayout);
        painelBotoesLayout.setHorizontalGroup(
            painelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelBotoesLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(botaoEscolher)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(botaoCancelar)
                .addGap(24, 24, 24))
        );
        painelBotoesLayout.setVerticalGroup(
            painelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoEscolher)
                    .addComponent(botaoCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(painelTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addComponent(painelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(painelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }

    public String [][] preparaTabela(){
        Funcionario atual;
        String [][] tudo = new String[this.lista.size()][2];
        for (int i = 0 ; i < this.lista.size() ; i++) {
            atual = (Funcionario) lista.get(i);
            tudo[i][0] = atual.getNomeFuncionario();
            tudo[i][1] = atual.getArea();
        }
        return tudo;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JanelaListaFuncionario().setVisible(true);
            }
        });
    }

    // declaração de variáveis
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoEscolher;
    private javax.swing.JPanel painelBotoes;
    private javax.swing.JScrollPane painelDeRolagem;
    private javax.swing.JPanel painelTabela;
    private javax.swing.JTable tabelaResultado;
    private Controlador controlador;
    private List lista;

    /**
     * @param controlador the controlador to set
     */
    public void setControlador(Controlador controlador) {
        this.controlador = controlador;
    }

    public void setVisivel(boolean visivel) {
        setVisible(visivel);
    }
}
